<template>
  <!-- 页面包裹区域 -->
  <el-container>
    <!-- 页面的头部 -->
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png" />
        <span>杭州9期后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <!-- 页面的内容 -->
    <el-container>
      <!-- 内容的左侧边栏 什么时候width=64px,width=200px-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 切换菜单折叠 -->
        <div class="toggleMenu" @click="isCollapse=!isCollapse">|||</div>
        <!-- 左侧菜单 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse="isCollapse"
          router
          :collapse-transition="false"
          :default-active="defaultActive"
          active-text-color="#409eff">
          <!-- 可以展开的一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menusList" :key="item.id">
            <!-- 定义一级菜单的展示效果 -->
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单，不需要展开 -->
            <el-menu-item @click="setDefaultActive('/'+subItem.path)" :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
              <!-- 定义二级菜单的展示效果 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容的右侧主体 -->
      <el-main>
        <!-- 根据不同的路由规则来展示不同的组件,所有的首页的子路由全部显示在这个位置 -->
        <transition>
          <router-view></router-view>
        </transition>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menusList: [],
      // 图标的映射规则
      iconsList:{
        '125':'iconfont icon-users',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      // 控制菜单折叠
      isCollapse: false,
      // 控制激活项
      defaultActive: ''
    }
  },

  created () {
    this.getMenusList()
    // 获取本地的active，来设置具体谁高亮
    this.defaultActive = sessionStorage.getItem('active') || '/users'
  },

  methods: {
    // 退出的事件函数
    logout () {
      sessionStorage.removeItem("token")
      this.$router.push('/login')
    },
    // 获取左侧菜单
    async getMenusList () {
      const {data: res} = await this.$axios.get('menus')
      if(res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menusList = res.data
    },
    // 设置高亮
    setDefaultActive (path) {
      sessionStorage.setItem('active',path)
      this.defaultActive = path
    }
  }
};
</script>

<style scoped lang='less'>
.el-container{
  height: 100%;
  
  .el-header {
    background: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .logo {
      display: flex;
      align-items: center;
      span{
        font-size: 25px;
        color: #fff;
        margin-left: 8px;
      }
    }
  }

  .el-aside {
    background: #333744;
    .toggleMenu {
      width: 100%;
      height: 40px;
      line-height: 40px;
      text-align: center;
      background: #666;
      letter-spacing: 5px;
      color: #fff;
      cursor: pointer;
      user-select: none;
    }
  }

  .el-main {
    background: #eaedf1;
    width: 100%;
    overflow-x: hidden;
  }
}

.el-menu {
  border-right: none;
}

.iconfont {
  margin-right: 8px;
}

.v-enter {
  transform: translateX(50px);
  opacity: 0;
}

.v-enter-active {
  transition: all 0.7s;
}

.v-enter-to {
  transform: translateX(0px);
  opacity: 1;
}
</style>
